import java.io.*;
import java.util.*;
import java.math.*;

public class Main
{
    static int[] f = new int[ 1024 ];
    static int m;

    public static void work( BufferedReader in  ) throws IOException
    {
        String s, p;
        BigInteger a = BigInteger.valueOf( 0 ), b;
        int maxn;
        boolean flag = true;
        while ( flag && ( s = in.readLine( ) ) != null )
        {
            if ( s.length( ) == 0 )
                break;
            if ( (int)s.charAt( s.length( ) - 1 ) == 26 )
            {
                flag = false;
                s = s.substring( 0, s.length( ) - 1 );
            }
            p = "";
            for ( int i = 0; i < s.length( ); i++ )
                if ( (int)s.charAt( i ) > 0x20 )
                    p += s.charAt( i );
            maxn = 0;
            for ( int i = 0; i < p.length( ); i++ )
                if ( maxn < f[ (int)p.charAt( i ) ] )
                    maxn = f[ (int)p.charAt( i ) ];
            a = BigInteger.valueOf( 0 );
            for ( int i = maxn + 1; i <= m; i++ )
            {
                b = BigInteger.valueOf( 0 );
                for ( int j = 0; j < p.length( ); j++ )
                    b = ( b.multiply( BigInteger.valueOf( i ) ) ).add( BigInteger.valueOf( f[ (int)p.charAt( j ) ] ) );
                a = a.add( b );
            }
            System.out.println( a );
        }
    }

    public static void main( String args[] ) throws IOException
    {
        InputStreamReader inputReader = new InputStreamReader( System.in );
        BufferedReader in = new BufferedReader( inputReader );
        String s;
        boolean flag = false;
        while ( ( s = in.readLine( ) ) != null )
        {
            if ( s.length( ) == 0 ) continue;
            if ( (int)s.charAt( s.length( ) - 1 ) == 26 )
                s = s.substring( 0, s.length( ) - 1 );
            m = s.length( );
            for ( int i = 0; i < s.length( ); i++ )
                f[ (int)s.charAt( i ) ] = i;
            if ( flag )
                System.out.println( );
            flag = true;
            work( in );
        }
    }
}
